/**
 * Copyright 1998-2006, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
package ca.uvic.cs.cogz.mapping.core;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Manager for mapping types. Keeps track of what mapping types are visible in the view.
 * 
 * @author seanf
 */
public class MappingTypeManager {
	public enum MappingType {
		VERIFIED_MAPPING("Verified mapping"), TEMPORARY_MAPPING("Temporary mapping"), CANDIDATE_MAPPING("Candidate mapping");

		private String typeText;

		MappingType(String typeText) {
			this.typeText = typeText;
		}

		public String toString() {
			return typeText;
		}
	}

	private Hashtable<MappingType, Boolean> mappingTypeVisibilityTable;
	private List<ChangeListener> changeListeners;

	public MappingTypeManager() {
		this.mappingTypeVisibilityTable = new Hashtable<MappingType, Boolean>();
		for (MappingType mappingType : MappingType.values()) {
			mappingTypeVisibilityTable.put(mappingType, true);
		}

		this.changeListeners = new ArrayList<ChangeListener>();
	}

	public void addMappingTypeChangeListener(ChangeListener changeListener) {
		changeListeners.add(changeListener);
	}

	public void setVisible(MappingType mappingType, boolean visible) {
		mappingTypeVisibilityTable.put(mappingType, visible);
		fireMappingTypeChangeListeners(mappingType);
	}

	public boolean isVisible(MappingType mappingType) {
		return mappingTypeVisibilityTable.get(mappingType);
	}

	private void fireMappingTypeChangeListeners(MappingType mappingType) {
		ChangeEvent e = new ChangeEvent(mappingType);
		for (ChangeListener l : changeListeners) {
			l.stateChanged(e);
		}
	}
}
